﻿#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)

#include <stdio.h>

//数组nums包含从0到n的所有整数，但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗？
//LeetCode 消失的数字
//https://leetcode.cn/problems/missing-number-lcci/

/*(1)
int missingNumber(int* nums, int numsSize)
{
    int x = 0;
    int i = 0;
    for( i = 0 ; i < numsSize ; i++)
    {
        x^=nums[i];
    }
    for(i = 0 ; i< numsSize + 1 ; i++)
    {
        x^=i;
    }
    return x;
}*/


int missingNumber(int* nums, int numsSize)
{
    int x = (1 + numsSize) * numsSize / 2;
    int i = 0;
    for (i = 0; i < numsSize ; i++)
    {
        x -= nums[i];
    }
    return x;
}

int main()
{
    int arr[] = { 9,6,4,2,3,5,7,0,1 };
    int x = missingNumber(arr, 9);
    printf("%d\n", x);
    return 0;
}